package com.hmdp.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.BooleanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Blog;
import com.hmdp.entity.User;
import com.hmdp.service.IBlogService;
import com.hmdp.service.IUserService;
import com.hmdp.utils.SystemConstants;
import com.hmdp.utils.ThreadLocalUtil;
import com.hmdp.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.hmdp.utils.RedisConstants.BLOG_LIKED_KEY;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@RestController
@Slf4j
@RequestMapping("/blog")
public class BlogController {

    @Resource
    private IBlogService blogService;
    @Resource
    private IUserService userService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping
    public Result saveBlog(@RequestBody Blog blog) {
        // 1.接收参数
        log.info("接收参数：{}", blog);
        // 2.业务:保存博文
        return blogService.saveBlog(blog);
    }

    private static Long getCurrentUserId() {
        /*获取用户信息*/
        UserDTO currentUser = (UserDTO) ThreadLocalUtil.get();
        Long currentUserId = currentUser.getId();
        return currentUserId;
    }

    @PutMapping("/like/{id}")
    public Result likeBlog(@PathVariable("id") Long id) {
        return blogService.likeBlog(id);
    }

    /**
     * 查看我的博文
     *
     * @param current
     * @return
     */
    @GetMapping("/of/me")
    public Result queryMyBlog(@RequestParam(value = "current", defaultValue = "1") Integer current) {
        return blogService.queryMyBlog(current);
    }

    /**
     * 查看热门博文列表
     *
     * @param current
     * @return
     */
    @GetMapping("/hot")
    public Result queryHotBlog(@RequestParam(value = "current", defaultValue = "1") Integer current) {
        return blogService.queryHotBlog(current);

    }

    /**
     * 根据博文id查询博文
     */
/*    @GetMapping("/{id}")
    public Result queryBlogByUserId (@PathVariable("id") Long id){
        //1.接收参数
        log.info("博文id：id={}", id);
        //2.业务:根据用户id查询博文
        Blog blog = blogService.query().eq(id != null, "id", id).one();//如果id不为空，则根据id查询博文
        if (blog == null) {
            log.error("博文不存在：id={}", id);
            return Result.fail("博文不存在");
        }
        //3.业务:查询博文用户消息
        User user = userService.getById(blog.getUserId());
        //3.1 设置用户消息
        blog.setName(user.getNickName());
        blog.setIcon(user.getIcon());
        //4.响应结果
        return Result.ok(blog);
    }*/

    /**
     * 根据博文id查询博文
     */
    @GetMapping("/{id}")
    public Result queryBlogByUserId(@PathVariable("id") Long id) {
        return blogService.queryBlogByUserId(id);
    }
    /**
     * 根据博文id查询该博文的用户点赞排行版信息(ZSET集合中的前五个用户的信息)
     * <p>
     * http://127.0.0.1:8081/blog/likes/24
     */
    @GetMapping("/likes/{id}")
    public Result queryBlogLikes(@PathVariable("id") Long id){
        log.info("查询点赞排行版信息：博文id={}", id);
        return blogService.queryBlogLikes(id);
    }


    /**
     * 查询博主发布的博文信息
     * @param current
     * @param id
     * @return
     */
    // BlogController  根据id查询博主的探店笔记
    @GetMapping("/of/user")
    public Result queryBlogByUserId(
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam("id") Long id) {
        // 根据用户查询
        Page<Blog> page = blogService.query()
                .eq("user_id", id).page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 获取当前页数据
        List<Blog> records = page.getRecords();
        return Result.ok(records);
    }

    /**
     * 实现关注推送页面的分页查询
     */
    @GetMapping("/of/follow")
    public Result queryBlogFollow(@RequestParam("lastId") Long maxTimeStamp, @RequestParam(value = "offset", defaultValue = "0") Integer offset) {
        log.info("queryBlogFollow 上一次每页查询的最小时间戳(也是这次查询的最大时间戳):{}, 偏移量:{}", maxTimeStamp, offset);
        return blogService.queryBlogFollow(maxTimeStamp, offset);
    }

}
